<!-- <template>
  <div id="app">
    <router-view />
  </div>
</template>
<script>
export default {
  created() {
    //建立连接
    this.goeasy.connect({
      id: "", // 推送类型 pubsub 选填，im必填  
      data: {}, // 必须是一个对象，pubsub 选填，im必填
      onSuccess: function () {  //连接成功
        console.log("连接成功") //连接成功
      },
      onFailed: function (error) { //连接失败
        console.log("连接失败:" + error.code + ",error:" + error.content);
      },
      onProgress: function (attempts) { //连接或自动重连中
        console.log("连接或自动重连中", attempts);
      }
    });
  },
  methods: {
    sub_mess() {
      // var pubsub = this.goeasy.pubsub;
      this.goeasy.pubsub.subscribe({
        channel: "my_channel",// 前后端的 channel 的一模一样
        onMessage: function (message) {
          //收到消息
          console.log("Channel:" + message.channel + " content:" + message.content);
        },
        onSuccess: function () {
          console.log("Channel订阅成功。");
        },
        onFailed: function (error) {
          console.log("Channel订阅失败, 错误编码：" + error.code + " 错误信息：" + error.content)
        }
      });
    }
  }
}
</script>
<style lang="less"></style> -->


<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>

export default {
  created() {
    // 订单数存入本地缓存 不易丢失
    const order_num = localStorage.getItem("order_num")
    if (!order_num || order_num < 1) {
      localStorage.setItem('order_num', 0)
    }
    // 建立连接
    this.goeasy.connect({
      id: "", //pubsub选填，im必填
      data: { "avatar": "/www/xxx.png", "nickname": "Neo" }, //必须是一个对象，pubsub选填，im必填，用于上下线提醒和查询在线用户列表时，扩展更多的属性
      onSuccess: () => {  //连接成功
        console.log("连接成功") //连接成功
        this.sub_message()
      },
      onFailed: (error) => { //连接失败
        console.log("连接失败:" + error.code + ",error:" + error.content);
      },
      onProgress: (attempts) => { //连接或自动重连中
        console.log("连接或自动重连中", attempts);
      }
    });
  },
  methods: {
    // 接收订阅消息
    sub_message() {
      var pubsub = this.goeasy.pubsub;
      pubsub.subscribe({
        channel: "my_channel",//替换为您自己的channel
        onMessage: (message) => {
          // 消息提示
          const order_num_a = localStorage.getItem("order_num")
          localStorage.setItem('order_num', Number(order_num_a) + 1)
          const order_num_b = localStorage.getItem("order_num")
          this.$store.commit('order_remind', Number(order_num_b))
          console.log("收到消息:" + message.channel + ":" + message.content); // 小程序发来的消息
        },
        onSuccess: () => {
          console.log("Channel订阅成功。");
        },
        onFailed: (error) => {
          console.log("Channel订阅失败, 错误编码：" + error.code + " 错误信息：" + error.content)
        }
      });
    }
  }
}
</script>

<style>
.el-loading-mask {
  left: 200px !important;
}

.image-view-title img {
  object-fit: cover;
}
</style>

